<template>
  <div>
    <el-menu :collapse="isCollage" default-active="2" class="el-menu-vertical-demo" background-color="#F5F5F5" text-color="#696969" active-text-color="red">
      <el-menu-item :index="item.path" v-for="item in noChildren" :key="item.path" @click="clickMenu(item)">
        <i :class="'el-icon-' + item.icon"></i>
        <span slot="title">{{ item.label }}</span>
      </el-menu-item>
      <el-submenu :index="item.path" v-for="(item, index) in hasChildren" :key="index">
        <template slot="title">
          <i :class="'el-icon-' + item.icon"></i>
          <span slot="title">{{ item.label }}</span>
        </template>
        <el-menu-item-group>
          <el-menu-item :index="item.path" v-for="(subItem, subIndex) in item.children" :key="subIndex" @click="clickMenu(subItem)">
            <i :class="'el-icon-' + subItem.icon"></i>
            <span slot="title">{{ subItem.label }}</span>
          </el-menu-item>
        </el-menu-item-group>
      </el-submenu>
    </el-menu>
  </div>
</template>

<script>
export default {
  computed: {
    noChildren() {
      return this.menu.filter((item) => !item.children) // 返回没有 item.children的item
    },
    hasChildren() {
      return this.menu.filter((item) => item.children)
    },
    isCollage() {
      return this.$store.state.tab.isCollapse
    },
    menu() {
      return this.$store.state.tab.menu
    },
  },
  data() {
    return {
      asideMenu: [
        { path: '/', name: 'home', label: '首页', icon: 's-home', type: 'danger' },
        { path: '/video', name: 'video', label: '视频管理', icon: 'video-play', type: 'danger' },
        { path: '/user', name: 'user', label: '用户管理', icon: 'user', type: 'danger' },
        {
          path: '/other',
          label: '其他',
          icon: 'user',
          children: [
            { path: '/page1', name: 'page1', label: '子页面1', icon: 'setting', type: 'danger' },
            { path: '/page2', name: 'page2', label: '子页面2', icon: 'setting', type: 'danger' },
          ],
        },
      ],
    }
  },
  methods: {
    clickMenu(item) {
      this.$router.push({ name: item.name })
      this.$store.commit('selectMenu', item)
    },
  },
}
</script>

<style lang="scss" scoped>
.el-menu {
  height: 100vh;
}
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 240px;
  min-height: 400px;
}
</style>
